#!/bin/bash

# 切換為root執行安裝
[ "`id -u`" != "0" ] && exec sudo su -c "$0" "$@"

# 變數宣告

Work_Path=`dirname "$0"`

if [ -f $Work_Path/install_func.sh ];then
  Work_Path=`cd "$Work_Path"; pwd`
elif [ -f $Work_Path/bin/install_func.sh ];then
  Work_Path=`cd "$Work_Path/bin"; pwd`;
else
  echo "Import install-function-library error !!! ";
  exit 1;
fi

# Log to /var/log/nutchez/shell-logs
source "$Work_Path/log.sh" master_install;
# Source functions
source "$Work_Path/install_func.sh";
### real code #####

main () {

  show_info "$MI_main_echo_1"
  set_install_information
  show_master_info
  show_info "$MI_main_echo_2"
  read confirm
  if [ $confirm -eq 1 ]; then
    creat_nutchuser_account $Nutchuser_Passwd
    make_ssh_key
    # 解壓縮
    unzip_nV2_pack
    # 建立系統所需資料夾
    mkdir_Home_Var
    # change /etc/hosts to nutchuser 
    change_hosts_owner
    #  
    install_Nutch
    #
    link_Chown   
 
    # 將網頁管理預設密碼寫入隱藏檔, 供網頁預設登入密碼使用
    su nutchuser -c "echo "nutchuser" > /home/nutchuser/nutchez/system/.passwd"
    chmod 600 /home/nutchuser/nutchez/system/.passwd
    make_client_install
    # 啟動系統
    format_HDFS
    start_up_NutchEZ
    start_up_tomcat    
    # 安裝流程結束，並進入網頁管理頁面設定爬網網址...等  
    show_info "$MI_main_echo_3"
#   show_info "Install Successfully!!"
    show_info "$MI_main_echo_4$MasterIP_Address:8080"
#   show_info "Visit http://$MasterIP_Address:8080"
    client_install_commands
  elif [ $confirm -eq 2 ]; then
    main
  else
    exit 0
  fi
}

load_default_lang
check_info
main
generateReadme
change_ownship nutchuser /home/nutchuser/nutchez
show_info $nutchez_install_finish
read
